package
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class InfoView extends MovieClip
	{
		private var loader:LoaderEvent;
		private var bgClass:Class;
		private var titleClass:Class;
		private var listClass:Class;
		private var pageCountClass:Class;
		private var messageClass:Class;
		private var pagePrevClass:Class;
		private var pageNextClass:Class;
		private var _bg:MovieClip;
		private var _titleItem:MovieClip;
		private var _listItem:MovieClip;
		private var _pageText:MovieClip;
		private var _message:MovieClip;
		private var _pagePrev:MovieClip;
		private var _pageNext:MovieClip;
		private var listArray:Array;
		private var objTemp:Array;
		private var _infodata:InfoData;
		private var pageNumber:uint = 5;
		private var pageCurrent:uint;
		private var pageTotal:uint;
		private var createNumber:uint;
		private var residue:uint;
		
		public function InfoView(url:String,infodata:InfoData)
		{
			_infodata = infodata;
			loader = new LoaderEvent();
			loader.load(url,LoaderEvent.CHILD);
			loader.addEventListener (Event.COMPLETE, loadSWFComplete);
		}
		
		private function loadSWFComplete(e:Event):void
		{
			//取得SWF中的MovieClip对应的类
			bgClass = loader.getClass("bg");
			titleClass = loader.getClass("titleItem");
			listClass = loader.getClass("listItem");
			pageCountClass = loader.getClass("pageCount");
			pagePrevClass = loader.getClass("pagePrev");
			pageNextClass = loader.getClass("pageNext");
			messageClass = loader.getClass("message");
			
			listArray = [];
			
			initialize();
		}
		
		private function initialize():void
		{
			_bg = new bgClass();
			_bg.x = _bg.y = 0;
			this.addChild(_bg);
			//创建titleItem
			for(var i:uint=0;i<_infodata.itemObj.length;i++)
			{
				_titleItem = new titleClass();
				_titleItem.x = 3 + _titleItem.width * i;
				_titleItem.y = 7;
				_titleItem.name = _infodata.itemObj[i].item;
				_titleItem.buttonMode = true;
				_titleItem.titleText.mouseEnabled = false;
				_titleItem.titleText.text = _infodata.itemObj[i].item;
				_titleItem.gotoAndStop("UP");
				this.addChild(_titleItem);
				_titleItem.addEventListener(MouseEvent.MOUSE_OVER,overState);
				_titleItem.addEventListener(MouseEvent.MOUSE_OUT,upState);
				_titleItem.addEventListener(MouseEvent.CLICK,selectListItem);
			}
			//创建listItem
			for(var n:uint=0;n<pageNumber;n++)
			{
				_listItem = new listClass();
				_listItem.x = 5;
				_listItem.y = 50 + _listItem.height * n;
				_listItem.gotoAndStop("UP");
				_listItem.itemText.mouseEnabled = false;
				this.addChild(_listItem);
				listArray.push(_listItem);
			}
			
			_pageText = new pageCountClass();
			_pageText.x = 37;
			_pageText.y = 305;
			this.addChild(_pageText);
			
			_pagePrev = new pagePrevClass();
			_pagePrev.x = 8;
			_pagePrev.y = 310;
			_pagePrev.gotoAndStop("ENABLE");
			_pagePrev.buttonMode = true;
			this.addChild(_pagePrev);
			_pageNext = new pageNextClass();
			_pageNext.x = 105;
			_pageNext.y = 310;
			_pageNext.gotoAndStop("ENABLE");
			_pageNext.buttonMode = true;
			this.addChild(_pageNext);
			
			_message = new messageClass();
			_message.x = 170;
			_message.y = 50;
			this.addChild(_message);
		}
		//点击titleItem后做出选择的判断
		private function selectListItem(e:MouseEvent):void
		{
			e.target.removeEventListener(MouseEvent,selectListItem);
			pageCurrent = 1;
			
			/*选择判断方法一
			switch(e.currentTarget.name)
			{
				case String(_infodata.itemObj[0].item):
					judgePage(_infodata.listObj[0]);
					break;
				case String(_infodata.itemObj[1].item):
					judgePage(_infodata.listObj[1]);
					break;
			}*/
			
			//选择判断方法二
			for(var i:uint=0;i<_infodata.itemObj.length;i++)
			{
				if(e.target.name == _infodata.itemObj[i].item)
				{
					judgePage(_infodata.listObj[i]);
				}
			}
		}
		
		private function judgePage(obj:Array):void
		{
			objTemp = obj;
			residue = objTemp.length%pageNumber;
			if(objTemp.length>pageNumber)
			{
				if(residue==0)
				{
					pageTotal = objTemp.length / pageNumber;
				}
				else
				{
					pageTotal = (objTemp.length-residue)/pageNumber+1;
				}
				createNumber = pageNumber;
			}
			else
			{
				pageTotal = 1;
				createNumber = objTemp.length;
			}
			updatePage();
		}
		
		private function updatePage():void
		{
			if(pageCurrent == 1)
			{
				_pagePrev.gotoAndStop("DISABLE");
				_pagePrev.removeEventListener(MouseEvent.CLICK,toPrev);
			}
			else
			{
				_pagePrev.gotoAndStop("ENABLE");
				_pagePrev.addEventListener(MouseEvent.CLICK,toPrev);
			}
			if(pageCurrent == pageTotal)
			{
				_pageNext.gotoAndStop("DISABLE");
				_pageNext.removeEventListener(MouseEvent.CLICK,toNext);
			}
			else
			{
				_pageNext.gotoAndStop("ENABLE");
				_pageNext.addEventListener(MouseEvent.CLICK,toNext);
			}
			_pageText.pageText.text = pageCurrent + "/" + pageTotal;
			
			addValue();
		}
		
		private function addValue():void
		{
			/*
			  如果当前页数等于所有页数，判断余数如果为零，创建list个数则等于每页个数，
			 否则余数不为零时，创建list个数则等于余数。如果当前页数不等于所有页数，
			创建list个数则等于每页个数
			*/
			
			if(pageCurrent==pageTotal)
			{
				if(residue==0)
				{
					createNumber = pageNumber;
				}else
				{
					createNumber = residue;
				}
			}else
			{
				createNumber = pageNumber;
			}
			
			var t:uint = (pageCurrent-1)*pageNumber;
			for(var i:uint=0;i<createNumber;i++)
			{
				listArray[i].buttonMode = true;
				listArray[i].itemText.text = objTemp[t+i].list;
				listArray[i].addEventListener(MouseEvent.MOUSE_OVER,overState);
				listArray[i].addEventListener(MouseEvent.MOUSE_OUT,upState);
				listArray[i].addEventListener(MouseEvent.CLICK,showMessage);
				listArray[i].name = t+i;
			}
			if(createNumber!=pageNumber)
			{
				for(var k:uint=createNumber;k<pageNumber;k++)
				{
					listArray[k].itemText.text = "";
					listArray[k].buttonMode = false;
					listArray[k].removeEventListener(MouseEvent.MOUSE_OVER,overState);
					listArray[k].removeEventListener(MouseEvent.MOUSE_OUT,upState);
					listArray[k].removeEventListener(MouseEvent.CLICK,showMessage);
				}
			}
		}
		
		private function showMessage(e:MouseEvent):void
		{
			var n:uint = uint(e.target.name);
			_message.messageText.text = objTemp[n].message;
		}
		
		private function toPrev(e:MouseEvent):void
		{
			pageCurrent--;
			updatePage();
		}
		
		private function toNext(e:MouseEvent):void
		{
			pageCurrent++;
			updatePage();
		}
		
		private function overState(e:MouseEvent):void
		{
			e.currentTarget.gotoAndStop("OVER");
		}
		
		private function upState(e:MouseEvent):void
		{
			e.currentTarget.gotoAndStop("UP");
		}
	}
}